import sqlite3


class SQLiteManager:
    def __init__(self, db_name='log_time_sleeps/main.db'):
        self.db_name = db_name
        self.db_connet()

    def db_connet(self):
        self.conn = sqlite3.connect(self.db_name, check_same_thread=False)
        self.cur = self.conn.cursor()

    def update_data(self, url, cookies, id):
        """部分更新数据"""
        update_sql = "UPDATE system_info SET url=?,cookies=? WHERE id=?"
        self.cur.execute(update_sql, (url, cookies, id))
        self.conn.commit()
    def update_cookies(self, url, cookies):
        update_sql = "UPDATE system_info SET cookies=? WHERE url=?"
        self.cur.execute(update_sql, (cookies,url))
        self.conn.commit()
    def select_data(self):
        """查询数据"""
        select_sql = "SELECT * FROM system_info"
        self.cur.execute(select_sql)
        rows = self.cur.fetchall()
        return rows


    def select_cookies(self, zhi):
        select_sql = f"SELECT * FROM system_info WHERE name =='{zhi}' AND  status=='active';"
        self.cur.execute(select_sql)
        rows = self.cur.fetchall()
        # self.close()
        return rows

    def select_by_id(self):
        select_sql = "PRAGMA table_info(system_info);"
        self.cur.execute(select_sql)
        rows = self.cur.fetchall()
        return rows


    def close(self):
        """关闭连接"""
        self.cur.close()
        self.conn.close()
